package com.hongye.app.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.hongye.app.model.BuyerStatisticsModel;
import com.hongye.app.model.EvaluationModel;
import com.hongye.app.model.FavoriteModel;
import com.hongye.app.model.GoodsCategroyModel;
import com.hongye.app.model.GoodsModel;
import com.hongye.app.model.PhoneCodeModel;
import com.hongye.app.model.Real_nameModel;
import com.hongye.app.model.TakeOutModel;
import com.hongye.app.model.UserAppModel;
import com.hongye.app.model.ViewDetailModel;
import com.hongye.util.JuheUtils;
import com.hongye.util.Utils;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;

/**
 * 店铺相关接口
 * @author liushaomin
 */
public class UserShopController extends Controller{

	public static String ImgPath = "http://120.27.117.192/rbg/upload/";//服务器图片路径
	private Logger log = Logger.getLogger(getClass());
	public UserShopController() {
		
	}
	
	public UploadFile getfile(String fileName){
		int maxPostSize = 20 * 1024 * 1024;
		return this.getFile(fileName, "", maxPostSize);
	}
	
	/**
	 * 创建店铺第一步 提交店铺信息
	 * @author liushaomin
	 * 2017年2月22日
	 */
	@Clear
	public void createShop_info() {
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("state", "fal");
		UploadFile avatar = getfile("avatar");
		String userphone = this.getPara("phone");
		String name = this.getPara("name");
		String explain = this.getPara("explain");
		String saletype_id = this.getPara("saletype_id");
		String address = this.getPara("address");
		String postage_type = this.getPara("postage_type");
		String mangou_money = this.getPara("mangou_money");
		String postage_money = this.getPara("postage_money");
		boolean boo = false;
		List<BuyerStatisticsModel> shop = BuyerStatisticsModel.getShopByName(name);
		if(shop.size() == 0){
			BuyerStatisticsModel user_tatistics = BuyerStatisticsModel.getStatistics(userphone);
			boo = user_tatistics.set("shop_avatar", avatar.getFileName()).set("shop_name", name).set("shop_explain", explain).set("shop_saletype", saletype_id).set("shop_address", address)
				  .set("seller_credit", 0.0).set("shop_paytype", 2).set("postage_type", postage_type).set("mangou_money", Double.valueOf(mangou_money)).set("postage_money", Double.valueOf(postage_money)).update();
		}else{
			rs.put("state", "shopname_repeat");
		}
		if(boo){
			rs.put("state", "suc");
		}
		this.renderJson(rs);
	}
	
	/**
	 * 创建店铺第二步 提交银行卡号
	 * 修改绑定的银行卡第一步 也调用该接口
	 * @author liushaomin
	 * 2017年2月22日
	 */
	@Clear
	public void createShop_bankcard() {
		Map<String, Object> rs = new HashMap<String, Object>();
		String userphone = this.getPara("phone");
		String bankcard = this.getPara("bankcard").replaceAll(" ", "");
		String type = this.getPara("type");
		Map<String, Object> result = JuheUtils.bankcardsilk(bankcard);
		String msg_code = result.get("res").toString();
		if(msg_code.equals("1")){
			BuyerStatisticsModel user_tatistics = BuyerStatisticsModel.getStatistics(userphone);
			if(type.equals("add")){
				user_tatistics.set("shop_bankcard", bankcard).set("shop_banktype", result.get("bank")).update();
			}
			rs.put("bankcardtype", result.get("type"));
			rs.put("banktype", result.get("bank"));
			rs.put("logo", result.get("logo"));
			PhoneCodeModel phonecode = new PhoneCodeModel();
			phonecode.set("token", Utils.getRandomString(4)).save();
			rs.put("tokenid", phonecode.get("id"));
			rs.put("token", phonecode.get("token"));
		}
		rs.put("state", msg_code);
		this.renderJson(rs);
	}
	
	/**
	 * 选择店铺经营范围获取所有商品类别
	 * @author lifuqiang
	 * 2017-2-28
	 */
	@Clear
	public void getGoodsCateAll(){
		String sql = "select c_name, id from goods_categroy where showg=0 and pid<100 order by pid";
		List<GoodsCategroyModel> list = GoodsCategroyModel.dao.find(sql);
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("goodscategroy", list);
		this.renderJson(rs);
	}
	
	/**
	 * 创建店铺第三步 校验银行卡信息
	 * 修改绑定的银行卡第二步 也调用该接口
	 * @author liushaomin
	 * 2017年2月22日
	 * @return 0：无法验证或不支持的银行；1：认证信息匹配；2：认证信息不匹配；-1：验证码错误；4身份证号码填写错误：验证码错误；101：服务器异常！
	 */
	@Clear
	public void createShop_bankcard_vail() {
		Map<String, Object> rs = new HashMap<String, Object>();
		String userphone = this.getPara("phone");
		String bankcard_name = this.getPara("bankcard_name");
		String bankcard_idcard = this.getPara("bankcard_idcard");
		String bankcard_phone = this.getPara("bankcard_phone");
		String code = this.getPara("code");
		String type = this.getPara("type");
		String bankcardid = this.getPara("bankcardid");
		String banktype = this.getPara("banktype");
		rs.put("state", "-1");
		if(UserAppModel.chkAppSMS(bankcard_phone, code, "bankcard")){//校验验证码
			BuyerStatisticsModel user_tatistics = BuyerStatisticsModel.getStatistics(userphone);
			Map<String, Object> result = new HashMap<String, Object>();
			if(type.equals("add")){
				result = JuheUtils.verifybankcard4(bankcard_name, bankcard_idcard, user_tatistics.getStr("shop_bankcard"), bankcard_phone);
			}else{
				result = JuheUtils.verifybankcard4(bankcard_name, bankcard_idcard, bankcardid.replaceAll(" ", ""), bankcard_phone);
			}
			if(log.isInfoEnabled()){log.info("verifybankcard4 result=" + result);}
			String msg_code = result.get("res").toString();
			if(msg_code.equals("1")){
				boolean boo = false;
				if(type.equals("add")){
					boo = user_tatistics.set("isshop", 1).set("shop_c_time", Utils.getDate()).update();
				}else if(type.equals("update")){
					boo = user_tatistics.set("shop_bankcard", bankcardid.replaceAll(" ", "")).set("shop_banktype", banktype).update();
				}
				if(boo){
					rs.put("state", msg_code);
				}else{
					rs.put("state", "101");
					if(log.isInfoEnabled()){log.info("createShop_bankcard_vail BuyerStatisticsModel update is fail");}
				}
			}else if(msg_code.equals("3") || msg_code.equals("6")){
				rs.put("state", "101");
			}else{
				rs.put("state", msg_code);
			}
		}
		this.renderJson(rs);
	}
	
	/**
	 * 我的店铺
	 * @author liushaomin
	 * 2017年2月27日
	 */
	@Clear
	public void myshop() {
		String phone = this.getPara("phone");
		BuyerStatisticsModel user_tatistics = BuyerStatisticsModel.getStatistics(phone);
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("shop_name", user_tatistics.get("shop_name"));
		rs.put("shop_avatar", UserAppInfoController.ImgPath + user_tatistics.get("shop_avatar"));
		rs.put("shop_credit", user_tatistics.getFloat("seller_credit"));//评分
		int useruid = user_tatistics.getInt("buyer_id");
		rs.put("total_money", BuyerStatisticsModel.getTotal_money(phone));//钱包
		rs.put("look_num", ViewDetailModel.getLook_Num(phone));//浏览量
		rs.put("unaudited_num", GoodsModel.getUnaudited_Num(useruid));//未审核的数量
		rs.put("invalid_num", GoodsModel.getInvalid_Num(useruid));//已下架的数量
		rs.put("fensishu", FavoriteModel.getFavoriteSize(phone));//粉丝数
		rs.put("isreal", user_tatistics.getInt("isrealname"));//是否认证
		String shop_saletype = user_tatistics.getStr("shop_saletype");
		String[] saletypeids = shop_saletype.split(",");
		List<Map<String ,Object>> data = new ArrayList<Map<String, Object>>();
		for (String item : saletypeids) {
			Map<String, Object> pra = new HashMap<String, Object>();
			int pid = GoodsCategroyModel.getCategroyName(item).getInt("pid");
			pra.put("pid", pid);
			pra.put("name", GoodsCategroyModel.getCategroyName(item).getStr("c_name"));
			pra.put("num", GoodsModel.getCategroyGoods_Num(useruid, pid));
			data.add(pra);
		}
		rs.put("data", data);
		this.renderJson(rs);
	}
	
	/**
	 * 评分详细
	 * @author liushaomin 
	 * 2017年5月16日
	 */
	@Clear
	public void shop_credit_detail() {
		String phone = this.getPara("phone");
		BuyerStatisticsModel user_tatistics = BuyerStatisticsModel.getStatistics(phone);
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("shop_credit", user_tatistics.getFloat("seller_credit"));//总评分
		UserAppModel user = UserAppModel.getByPhone(phone);
		int seller_id = user.getInt("user_id");
		rs.put("service_level", EvaluationModel.average("service_level", seller_id));//服务评分
		rs.put("logistic_level", EvaluationModel.average("logistic_level", seller_id));//物流评分
		rs.put("goods_level", EvaluationModel.average("goods_level", seller_id));//商品描述评分
		rs.put("linechart", EvaluationModel.linechart(3, seller_id));//折线图
		this.renderJson(rs);
	}
	
	/**
	 * 获取商家已下架或未审核的商品
	 * @author liushaomin
	 * 2017年3月2日
	 */
	@Clear
	public void other_goods() {
		String type = this.getPara("type");
		String phone = this.getPara("phone");
		String pageNumber = this.getPara("pageNumber");
		Page<GoodsModel> goods = GoodsModel.getOther_Goods(phone, type, Integer.parseInt(pageNumber));
		List<GoodsModel> list = goods.getList();
		for (GoodsModel goodsModel : list) {
			goodsModel.set("thumb", UserAppInfoController.ImgPath + goodsModel.getStr("thumb"));
		}
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("data", goods);
		this.renderJson(rs);
	}
	
	
	/**
	 * 获取类别商品列表
	 * @author liushaomin
	 * 2017年2月28日
	 */
	@Clear
	public void goodstype() {
		String phone = this.getPara("phone");
		int pid = this.getParaToInt("pid");
		String pageNumber = this.getPara("pageNumber");
		Map<String, Object> rs = new HashMap<String, Object>();
		Page<GoodsModel> goods = GoodsModel.getGoodsListByPid(phone, pid, Integer.parseInt(pageNumber));
		List<GoodsModel> list = goods.getList();
		for (GoodsModel goodsModel : list) {
			goodsModel.set("thumb", UserAppInfoController.ImgPath + goodsModel.getStr("thumb"));
		}
		rs.put("data", goods);
		this.renderJson(rs);
	}
	
	/**
	 * 编辑店铺资料时 获取店铺信息
	 * @author liushaomin
	 * 2017年3月3日
	 */
	@Clear
	public void myshop_info(){
		String phone = this.getPara("phone");
		BuyerStatisticsModel buyersta = BuyerStatisticsModel.getStatistics(phone);
		Map<String, Object> pare = new HashMap<String, Object>();
		pare.put("shop_avatar", UserAppInfoController.ImgPath + buyersta.getStr("shop_avatar"));
		pare.put("shop_name", buyersta.getStr("shop_name"));
		pare.put("shop_address", buyersta.getStr("shop_address"));
		pare.put("shop_explain", buyersta.getStr("shop_explain"));
		pare.put("shop_bankcard", buyersta.getStr("shop_bankcard"));
		pare.put("shop_banktype", buyersta.getStr("shop_banktype"));
		pare.put("postage_type", buyersta.getInt("postage_type"));
		pare.put("postage_typeName", BuyerStatisticsModel.getPostage_typeName(buyersta.getInt("postage_type"), buyersta.getDouble("mangou_money")));
		pare.put("mangou_money", buyersta.getDouble("mangou_money"));
		pare.put("postage_money", buyersta.getDouble("postage_money"));
		String[] saletypeids = buyersta.getStr("shop_saletype").split(",");
		List<Map<String ,Object>> data = new ArrayList<Map<String, Object>>();
		for (String item : saletypeids) {
			Map<String, Object> pra = new HashMap<String, Object>();
			pra.put("name", GoodsCategroyModel.getCategroyName(item).getStr("c_name"));
			data.add(pra);
		}
		pare.put("saletypeName", data);
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("shopinfo", pare);
		this.renderJson(rs);
	}
	
	/**
	 * 修改店铺头像
	 * @author lifuqiang
	 * 2017-3-7
	 */
	@Clear
	public void setShopAvatar(){
		UploadFile shop_avatar = getfile("shop_avatar");
		String phone = this.getPara("phone");
		String sql = "select * from buyer_statistics where buyer_phone = '"+phone+"'";
		BuyerStatisticsModel buyersta = BuyerStatisticsModel.dao.findFirst(sql);
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("state", "fal");
		if(buyersta.set("shop_avatar", shop_avatar.getFileName()).update()){
			rs.put("state", "suc");
		}
		this.renderJson(rs);
	}
	
	/**
	 * 修改店铺基本信息（包括店铺名称、所在地、店铺介绍、）
	 * @author lifuqiang
	 * 2017-3-1
	 */
	@Clear
	public void setShopBase(){
		String shop_name = this.getPara("shop_name");
		String shop_address = this.getPara("shop_address");
		String shop_explain = this.getPara("shop_explain");
		String phone = this.getPara("phone");
		String type = this.getPara("type");
		String postage_type = this.getPara("postage_type");
		String mangou_money = this.getPara("mangou_money");
		String postage_money = this.getPara("postage_money");
		String sql = "select * from buyer_statistics where buyer_phone = '"+phone+"'";
		BuyerStatisticsModel buyersta = BuyerStatisticsModel.dao.findFirst(sql);
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("state", "fal");
		boolean boo = false;
		if(type.equals("shop_name")){
			boo = buyersta.set("shop_name", shop_name).update();
		}else if(type.equals("shop_address")){
			boo = buyersta.set("shop_address", shop_address).update();
		}else if(type.equals("shop_explain")){
			boo = buyersta.set("shop_explain", shop_explain).update();
		}else if(type.equals("postage_type")){
			boo = buyersta.set("postage_type", postage_type).set("mangou_money", mangou_money).set("postage_money", postage_money).update();
		}
		if(boo){
			rs.put("state", "suc");
		}
		this.renderJson(rs);
	}
	
	/**
	 * 他的店铺
	 * @author liushaomin
	 * 2017年4月12日
	 */
	@Clear
	public void hisshop(){
		String hisphone = this.getPara("hisphone");
		String mephone = this.getPara("mephone");
		BuyerStatisticsModel Buser = BuyerStatisticsModel.getStatistics(hisphone);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("shop_avatar", ImgPath + Buser.get("shop_avatar"));
		map.put("shop_name", Buser.get("shop_name"));
		map.put("shop_credit", Buser.get("seller_credit"));
		map.put("user_phone", hisphone);
		map.put("fensishu", FavoriteModel.dao.find("select * from favorite where to_phone = '"+ hisphone +"'").size());
		map.put("guanzhushu", FavoriteModel.dao.find("select * from favorite where from_phone = '"+ hisphone +"'").size());
		String sqlfa3 = "select * from favorite where from_phone = '"+mephone+"'and to_phone = '"+hisphone+"'";
		FavoriteModel favorite = new FavoriteModel().findFirst(sqlfa3);
		if(favorite == null){
			map.put("iszhushu", false);
		}else{
			map.put("iszhushu", true);
		}
		GoodsModel goods = GoodsModel.dao.findFirst("SELECT count(1) as count FROM goods WHERE seller_id = "+Buser.getInt("buyer_id")+" and sale = 0 and audit = 1");
		map.put("goodscount", goods.get("count"));
		map.put("postage_type", Buser.getInt("postage_type"));
		map.put("mangou_money", Buser.getDouble("mangou_money"));
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("data", map);
		if(log.isInfoEnabled()){log.info(""+rs);}
		this.renderJson(rs);
	}
	
	/**
	 * 获取ta人的店铺商品列表接口（分页）
	 * @author liushaomin
	 * 2017年4月12日
	 */
	@Clear
	public void getHisShopGoods(){
		Page<GoodsModel> goods_list = GoodsModel.dao.getHisShopGoods(getPara("phone"), getParaToInt("pageNumber"));
		for (GoodsModel item : goods_list.getList()) {
			item.set("thumb", ImgPath + item.get("thumb"));
			item.put("shop_address", BuyerStatisticsModel.getAddress(item.getStr("seller_id")));
		}
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("goods", goods_list);
		this.renderJson(rs);
	}
	
	/**
	 * 进入钱包页面
	 * @author liushaomin
	 * 2017年4月19日
	 */
	@Clear
	public void mywallet(){
		String phone = this.getPara("phone");
		BuyerStatisticsModel buyermodel = BuyerStatisticsModel.getBuyerWallet(phone);
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("wallet", buyermodel);
		this.renderJson(rs);
	}
	
	/**
	 * 钱包明细--列表-分页
	 * @author lifuqiang
	 * 2017-4-19
	 */
	@Clear
	public void walletdetail_list(){
		String phone = this.getPara("phone");
		int pageNumber = this.getParaToInt("pageNumber");
		Page<TakeOutModel> walletdetail_list =  TakeOutModel.TakeOutDetailList_By_phone(phone,pageNumber);
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("detail_list", walletdetail_list);
		this.renderJson(rs);
	}
	
	/**
	 * 钱包明细--详情
	 * @author lifuqiang
	 * 2017-4-19
	 */
	@Clear
	public void walletdetail(){
		String id = this.getPara("id");
		TakeOutModel walletdetail =  TakeOutModel.TakeOutDetail_By_id(id);
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("walletdetail", walletdetail);
		this.renderJson(rs);
	}
	
	/**
	 * 准备申请提现
	 * @author lifuqiang
	 * 2017-4-19
	 */
	@Clear
	public void planApplyTakeout(){
		String phone = this.getPara("phone");
		BuyerStatisticsModel bs = BuyerStatisticsModel.planApplyTakeout(phone);
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("planApply", bs);
		this.renderJson(rs);
	}
	
	/**
	 * 申请提现
	 * 减少可提现金额
	 * @author lifuqiang
	 * 2017-4-19
	 */
	@Clear
	public void ApplyTakeout(){
		String phone = this.getPara("phone");
		String money = this.getPara("money");
		String content = this.getPara("content");
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("state", "fal");
		double all_money = (Double)BuyerStatisticsModel.getStatistics(phone).get("shop_usable_account");
		boolean boo_isout = (Double.parseDouble(money) <= all_money) ? true : false ;//是否超出可提现总金额
		if(boo_isout){
			boolean boo = BuyerStatisticsModel.Update_Reduce_Usable(phone, Double.parseDouble(money));//减少可提现金额
			if(boo){
				BuyerStatisticsModel bs = BuyerStatisticsModel.getStatistics(phone);
				TakeOutModel to = new TakeOutModel();
				boolean boo_save = to.set("buyer_id", bs.get("buyer_id")).set("buyer_phone", phone).set("money", money).set("leave_money", bs.get("shop_usable_account"))
				.set("c_time", Utils.getDate()).set("type", 1).set("bankcard", bs.get("shop_bankcard")).set("banktype", bs.get("shop_banktype"))
				.set("content", content).set("status", 0).save();
				if(boo_save){
					rs.put("state", "suc");
				}
			}
		}else{
			rs.put("state", "out");
		}
		this.renderJson(rs);
	}
	
	/**
	 * 进入实名认证页面
	 * @author liushaomin
	 * 2017年5月4日
	 */
	@Clear
	public void RealName(){
		String phone = this.getPara("phone");
		Map<String, Object> pra = new HashMap<String, Object>();
			Real_nameModel realname = Real_nameModel.getRealName_json(phone);
			if(realname == null){//未认证
				pra.put("type", "1");
			}else{
				if(realname.getInt("state") ==1){//认证信息审核中
					pra.put("type", "2");
				}else if(realname.getInt("state") ==2){//实名认证未通过，请重新提交
					pra.put("type", "3");
				}else{//已实名认证
					pra.put("type", "4");
				}
				pra.remove("state");
				realname.put("idcard_just_img", ImgPath + realname.getStr("idcard_just_img"));
				realname.put("idcard_back_img", ImgPath + realname.getStr("idcard_back_img"));
				realname.put("license_img", ImgPath + realname.getStr("license_img"));
				pra.put("realname", realname);
			}
		this.renderJson(pra);
	}
	
	/**
	 * 提交认证信息
	 * @author liushaomin
	 * 2017年5月4日
	 */
	@Clear
	public void ApplyRealName(){
		UploadFile idcard_just = getfile("idcard_just");//法人身份证照片-正
		UploadFile idcard_back = getfile("idcard_back");//法人身份证照片-反
		UploadFile license_img = getfile("license_img");//营业执照照片
		String phone = this.getPara("phone");
		String type = this.getPara("type");
		String legal_person = this.getPara("legal_person");//企业法人
		String company_name = this.getPara("company_name");//企业名称
		String org_code = this.getPara("org_code");//机构代码证号(三证合一)
		String region = this.getPara("region");//所在地区
		String address = this.getPara("address");//详细地址
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("state", "fal");
		if(type.equals("0")){
			Real_nameModel realname = new Real_nameModel();
			boolean boo = realname.set("buyer_id", UserAppModel.getUserid(phone)).set("buyer_phone", phone).set("legal_person", legal_person).set("company_name", company_name).set("org_code", org_code)
					.set("region", region).set("address", address).set("idcard_just_img", idcard_just.getFileName()).set("idcard_back_img", idcard_back.getFileName()).set("license_img", license_img.getFileName())
					.set("ctime", Utils.getDate()).set("state", 1).save();
			if(boo){
				map.put("state", "suc");
			}
		}else{
			Real_nameModel realname = Real_nameModel.getRealName(phone);
			boolean boo = realname.set("legal_person", legal_person).set("company_name", company_name).set("org_code", org_code).set("region", region).set("address", address)
					.set("idcard_just_img", idcard_just.getFileName()).set("idcard_back_img", idcard_back.getFileName()).set("license_img", license_img.getFileName()).set("state", 1).update();
			if(boo){
				map.put("state", "suc");
			}
		}
		this.renderJson(map);
	}
	
	/**
	 * 按天查询浏览量——明细
	 * @author liushaomin
	 * 2017年6月6日
	 */
	@Clear
	public void lookdetail_day(){
		String phone = this.getPara("phone");
		Map<String, Object> rs = new HashMap<String, Object>();
		List<Map<String, Object>> list_detail = new ArrayList<Map<String,Object>>();
		String sql = "SELECT COUNT(*) AS num FROM viewdetail WHERE tophone = '"+ phone +"' AND ";
		String sql1 = "";
		long lately = 0;
		long count = 0;
		for(int i=0; i<7; i++){
			Map<String, Object> map_detail = new HashMap<String, Object>();
			sql1 = "TO_DAYS(NOW())-TO_DAYS(ctime)=" + i;
			count =  ViewDetailModel.getLook(sql + sql1);
			map_detail.put("time", Utils.getDate("day", i, "yyyy-MM-dd"));
			map_detail.put("count", count);
			list_detail.add(map_detail);
			if(i==0){
				lately = count;
			}
		}
		rs.put("detail", list_detail);
		rs.put("lately", lately + "次／天");
		this.renderJson(rs);
	}
	
	/**
	 * 按周查询浏览量——明细
	 * @author liushaomin
	 * 2017年6月12日
	 */
	@Clear
	public void lookdetail_week(){
		String phone = this.getPara("phone");
		Map<String, Object> rs = new HashMap<String, Object>();
		List<Map<String, Object>> list_detail = new ArrayList<Map<String,Object>>();
		String sql = "SELECT COUNT(*) AS num FROM viewdetail WHERE tophone = '"+ phone +"' AND ";
		String sql1 = "";
		long lately = 0;
		for(int i=1; i<8; i++){
			Map<String, Object> map_detail = new HashMap<String, Object>();
			 switch(i){
		        case 1:
		        	sql1 = "TO_DAYS(NOW())-TO_DAYS(ctime)<=7 and TO_DAYS(NOW())-TO_DAYS(ctime)>0";
		        	lately = ViewDetailModel.getLook(sql + sql1);
		        	map_detail.put("time", Utils.getDate("day", 1, "yyyy-MM-dd")+ "至" + Utils.getDate("day", 7, "yyyy-MM-dd"));
		            break;
		        case 2:
		        	sql1 = "TO_DAYS(NOW())-TO_DAYS(ctime)<=14 and TO_DAYS(NOW())-TO_DAYS(ctime)>7";
		        	map_detail.put("time", Utils.getDate("day", 7, "yyyy-MM-dd")+ "至" + Utils.getDate("day", 14, "yyyy-MM-dd"));
		            break;
		        case 3:
		        	sql1 = "TO_DAYS(NOW())-TO_DAYS(ctime)<=21 and TO_DAYS(NOW())-TO_DAYS(ctime)>14";
		        	map_detail.put("time", Utils.getDate("day", 14, "yyyy-MM-dd")+ "至" + Utils.getDate("day", 21, "yyyy-MM-dd"));
		            break;
		        case 4:
		        	sql1 = "TO_DAYS(NOW())-TO_DAYS(ctime)<=28 and TO_DAYS(NOW())-TO_DAYS(ctime)>21";
		        	map_detail.put("time", Utils.getDate("day", 21, "yyyy-MM-dd")+ "至" + Utils.getDate("day", 28, "yyyy-MM-dd"));
		            break;
		        case 5:
		        	sql1 = "TO_DAYS(NOW())-TO_DAYS(ctime)<=35 and TO_DAYS(NOW())-TO_DAYS(ctime)>28";
		        	map_detail.put("time", Utils.getDate("day", 28, "yyyy-MM-dd")+ "至" + Utils.getDate("day", 35, "yyyy-MM-dd"));
		            break;
		        case 6:
		        	sql1 = "TO_DAYS(NOW())-TO_DAYS(ctime)<=42 and TO_DAYS(NOW())-TO_DAYS(ctime)>35";
		        	map_detail.put("time", Utils.getDate("day", 35, "yyyy-MM-dd")+ "至" + Utils.getDate("day", 42, "yyyy-MM-dd"));
		            break;
		        case 7:
		        	sql1 = "TO_DAYS(NOW())-TO_DAYS(ctime)<=49 and TO_DAYS(NOW())-TO_DAYS(ctime)>42";
		        	map_detail.put("time", Utils.getDate("day", 42, "yyyy-MM-dd")+ "至" + Utils.getDate("day", 49, "yyyy-MM-dd"));
		            break;
		        }
			 map_detail.put("count", ViewDetailModel.getLook(sql + sql1));
			 list_detail.add(map_detail);
		}
		rs.put("detail", list_detail);
		rs.put("lately", lately + "次／周");
		this.renderJson(rs);
	}
	
	/**
	 * 按月查询浏览量——明细
	 * @author liushaomin
	 * 2017年6月12日
	 */
	@Clear
	public void lookdetail_month(){
		String phone = this.getPara("phone");
		Map<String, Object> rs = new HashMap<String, Object>();
		List<Map<String, Object>> list_detail = new ArrayList<Map<String,Object>>();
		String sql = "SELECT COUNT(*) AS num FROM viewdetail WHERE tophone = '"+ phone +"' AND ";
		String sql1 = "";
		long lately = 0;
		long count = 0;
		for(int i=1; i<8; i++){
			Map<String, Object> map_detail = new HashMap<String, Object>();
			sql1 = "MONTH(NOW())-MONTH(ctime)=" + i;
			count =  ViewDetailModel.getLook(sql + sql1);
			map_detail.put("time", Utils.getDate("month", i, "yyyy-MM"));
			map_detail.put("count", count);
			list_detail.add(map_detail);
			if(i==1){
				lately = count;
			}
		}
		rs.put("detail", list_detail);
		rs.put("lately", lately + "次／月");
		this.renderJson(rs);
	}
}
